package GUI;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import ctrLayer.ForestOwnerCtr;
import ctrLayer.TreeCtr;

public class CreateForestFrame extends JFrame {
	private ForestOwnerCtr fCtr;
	private TreeCtr tCtr;
	
	private ForestPanel forestPanel;
	
	private JTextField txtForestName;
	private JTextField txtForestLocation;
	private JTextField txtSquareKiloMetres;
	private JTextField txtNatureOfTrees;
	private JTextField txtForestPrice;
	
	private JComboBox comboBoxOwner;
	private JComboBox comboBoxTreeType;

	public CreateForestFrame() {
		fCtr = ForestOwnerCtr.getInstance();
		tCtr = TreeCtr.getInstance();
		initilizeFrame();
		forestPanel = ForestPanel.getInstance();
	}

	private void initilizeFrame() {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		
		setTitle("Create forest");
		setBounds((int) d.getWidth()/4, (int) d.getHeight() / 5, 365, 261);
		setResizable(false);
		getContentPane().setLayout(null);
		
		JLabel lblForestName = new JLabel("Name");
		lblForestName.setBounds(10, 15, 120, 20);
		getContentPane().add(lblForestName);
		
		txtForestName = new JTextField();
		txtForestName.setBounds(140, 15, 200, 20);
		getContentPane().add(txtForestName);
		txtForestName.setColumns(10);
		
		JLabel lblForestLocation = new JLabel("Location");
		lblForestLocation.setBounds(10, 40, 120, 20);
		getContentPane().add(lblForestLocation);
		
		txtForestLocation = new JTextField();
		txtForestLocation.setBounds(140, 40, 200, 20);
		getContentPane().add(txtForestLocation);
		txtForestLocation.setColumns(10);
		
		JLabel lblSquareKiloMetres = new JLabel("Squarekilometres");
		lblSquareKiloMetres.setBounds(10, 65, 132, 20);
		getContentPane().add(lblSquareKiloMetres);
		
		txtSquareKiloMetres = new JTextField();
		txtSquareKiloMetres.setBounds(140, 65, 200, 20);
		getContentPane().add(txtSquareKiloMetres);
		txtSquareKiloMetres.setColumns(10);
		
		JLabel lblNatureOfTress = new JLabel("Natureoftrees");
		lblNatureOfTress.setBounds(10, 90, 132, 20);
		getContentPane().add(lblNatureOfTress);
		
		txtNatureOfTrees = new JTextField();
		txtNatureOfTrees.setBounds(140, 90, 200, 20);
		getContentPane().add(txtNatureOfTrees);
		txtNatureOfTrees.setColumns(10);
		
		JLabel lblForestPrice = new JLabel("Price");
		lblForestPrice.setBounds(10, 115, 120, 20);
		getContentPane().add(lblForestPrice);
		
		txtForestPrice = new JTextField();
		txtForestPrice.setBounds(140, 115, 200, 20);
		getContentPane().add(txtForestPrice);
		txtForestPrice.setColumns(10);
		
		String[] owner = fCtr.listToArrayCombobox();
		comboBoxOwner = new JComboBox(owner);
		comboBoxOwner.setBounds(140, 140, 200, 20);
		getContentPane().add(comboBoxOwner);
		
		String[] tree = tCtr.listToArrayCombobox();
		comboBoxTreeType = new JComboBox(tree);
		comboBoxTreeType.setBounds(140, 165, 200, 20);
		getContentPane().add(comboBoxTreeType);
		
		JButton btnCreate = new JButton("Create");
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createForest();
			}
		});
		btnCreate.setBounds(254, 202, 86, 20);
		getContentPane().add(btnCreate);
		
		JLabel lblOwner = new JLabel("Owner");
		lblOwner.setBounds(10, 143, 46, 14);
		getContentPane().add(lblOwner);
		setVisible(true);
	}
	
	public void createForest() {
		boolean success = false;
		String name = txtForestName.getText();
		String location = txtForestLocation.getText();
		String squareKiloMetre1 = txtSquareKiloMetres.getText();
		String natureOfTrees = txtNatureOfTrees.getText();
		String price1 = txtForestPrice.getText();
		double price = Double.parseDouble(price1);
		int ownerNr =  comboBoxOwner.getSelectedIndex()+1;
		int treeId = comboBoxTreeType.getSelectedIndex()+1; 
		
		if(name.isEmpty() != true && location.isEmpty() != true && squareKiloMetre1.isEmpty() != true && natureOfTrees.isEmpty() != true && price1.isEmpty() != true) {
		success = fCtr.CreateForest(txtForestName.getText(), txtForestLocation.getText(), Double.parseDouble(txtSquareKiloMetres.getText()), txtNatureOfTrees.getText(), Double.parseDouble(txtForestPrice.getText()), comboBoxOwner.getSelectedIndex()+1, comboBoxTreeType.getSelectedIndex()+1 );
		close();
		forestPanel.updateTable();
		dispose();
		} else {
			JOptionPane.showMessageDialog(getContentPane(), "You have to fill in all textFields", "Error Message", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void close() {
		setVisible(false);
		dispose();
	}
}